package com.detrum.detrumdx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.detrum.base.BaseStatic;
import com.detrum.bean.VersionBean;
import com.detrum.bean.ViewPagerItem;
import com.detrum.detrumdx.R;
import com.detrum.utils.LogUtils;

import java.util.List;

/**
 * Created by abc on 2018/3/14.
 */

public class DeviceAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<VersionBean> beanList;
    private int size;

    public DeviceAdapter(Context context, List<VersionBean> beanList,int size) {
        this.context = context;
        this.beanList = beanList;
        this.size = size;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return beanList==null?0:size;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DeviceHolder holder;
        if(convertView==null){
            convertView = layoutInflater.inflate(R.layout.info_show,parent,false);
            holder = new DeviceHolder();
            holder.image = convertView.findViewById(R.id.info_image);
            holder.name = convertView.findViewById(R.id.info_name);
            holder.info = convertView.findViewById(R.id.info);
            holder.update = convertView.findViewById(R.id.update);
            convertView.setTag(holder);
        }else{
            holder = (DeviceHolder) convertView.getTag();
        }
        holder.name.setText(beanList.get(position).getName());
        String deviceVersion = beanList.get(position).getDeviceVersion();
        if(deviceVersion==null||deviceVersion.length()==0){
            return null;
        }
        if(beanList.get(position).getName().contains("IWT")){
            holder.image.setImageResource(R.mipmap.mslogo2);
        }else if(beanList.get(position).getName().equals("DCAM-H")){
            holder.image.setImageResource(R.mipmap.sport2);
        }
        holder.info.setText(beanList.get(position).getDeviceVersion());
        String DeviceVersion = beanList.get(position).getDeviceVersion();
        String DeviceNetVersion = beanList.get(position).getNetVersion();
        if(DeviceVersion!=null&&DeviceVersion.length()>10&&DeviceNetVersion!=null&&DeviceNetVersion.length()>10){
            long code = Long.parseLong(DeviceVersion.substring(6));
            long netCode = Long.parseLong(DeviceNetVersion.substring(6));
            LogUtils.i(code+"========"+netCode);
            if(DeviceVersion!=null&&DeviceNetVersion!=null&&code< netCode){
                holder.update.setVisibility(View.VISIBLE);
            }else{
                holder.update.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    class DeviceHolder{
        ImageView image,update;
        TextView name,info;
    }
}
